package com.hrsystem.controller;
import com.hrsystem.common.Res;
import com.hrsystem.entity.PersonalData;
import com.hrsystem.service.PersonalDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/personaldata")
public class PersonalDataController {

    @Autowired
    PersonalDataService personalDataService;

    /**
     * 添加祖宗第一代
     *
     * @param personalData
     * @return
     */
    @PostMapping("/intoancestors")
    public Res getIntoancestors(@RequestBody PersonalData personalData) {
        PersonalData intoChildren = personalDataService.getIntoancestors(personalData);
        System.out.println(personalData);
        if (intoChildren == null && intoChildren.getName() == null) {
            return Res.create(0, "添加子代失败！", null);
        } else {
            return Res.create(1, "添加子代成功!", intoChildren);
        }
    }

    /**
     * 添加子代
     *
     * @param personalData
     * @return
     */
    @PostMapping("/intochildren")
    public Res intoChildren(@RequestBody PersonalData personalData) {
        PersonalData intoChildren = personalDataService.intoChildren(personalData);
        System.out.println(personalData);
        if (intoChildren == null && intoChildren.getName() == null) {
            return Res.create(0, "添加子代失败！", null);
        } else {
            return Res.create(1, "添加子代成功!", intoChildren);
        }
    }

    /**
     * 添加配偶
     *
     * @param personalData
     * @return
     */
    @PostMapping("/intospouse")
    public Res intoSpouse(@RequestBody PersonalData personalData) {
        PersonalData intoSpouse = personalDataService.intoSpouse(personalData);
        System.out.println(personalData);
        if (intoSpouse == null && intoSpouse.getName() == null) {
            return Res.create(0, "添加配偶失败！", null);
        } else {
            return Res.create(1, "添加配偶成功!", intoSpouse);
        }
    }

    /**
     * 查询配偶信息
     *
     * @param id
     * @return
     */
    @GetMapping("/spouse")
    public Res checkSpouse(Integer id) {
        PersonalData personalData = personalDataService.checkSpouse(id);
        System.out.println("这是" + id);
        if (personalData != null) {
            return Res.create(1, "查询成功", personalData);
        }
        return Res.create(0, "查询失败", null);
    }

    /**
     * @param id
     * @return 查询个人信息
     */
    @GetMapping("/check")
    public Res check(Integer id) {
        PersonalData personalData = (PersonalData) personalDataService.check(id);
        if (personalData != null) {
            return Res.create(1, "查询成功", personalData);
        }
        return Res.create(0, "查询失败", null);
    }

    /**
     * 更新个人数据
     *(查阅族谱时)
     * @param id
     * @param personalData
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.PATCH, RequestMethod.PUT})
    // 两种传参方式混合使用，详见上面的两个代码
    // 对应前端写法为：this.$axios.patch('/api/user/1', this.user)，其中 1 表示用户ID为 1
    public Res updateUser(@PathVariable("id") Integer id, @RequestBody PersonalData personalData) throws Exception {
        // 要修改的用户以路径中的ID为准
        personalData.setId(id);
        if (personalDataService.updateUser(personalData) != null) {
            return Res.create(1, "修改成功", personalData);
        } else {
            return Res.create(0, "修改失败", null);
        }
    }

    /**
     * 更新个人数据
     * (管理族谱时)
     * @param id
     * @param personalData
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/manage/update/{id}", method = {RequestMethod.PATCH, RequestMethod.PUT})
    // 两种传参方式混合使用，详见上面的两个代码
    // 对应前端写法为：this.$axios.patch('/api/user/1', this.user)，其中 1 表示用户ID为 1
    public Res getUpdatePersonManage(@PathVariable("id") Integer id, @RequestBody PersonalData personalData) throws Exception {
        // 要修改的用户以路径中的ID为准
        personalData.setId(id);
        if (personalDataService.getUpdatePersonManage(personalData) != null) {
            return Res.create(1, "修改成功", personalData);
        } else {
            return Res.create(0, "修改失败", null);
        }
    }

    /**
     * 删除族员(管理族谱时)
     * @param personalData
     * @return
     * @throws Exception
     */
    @DeleteMapping(value = "/manage/delete/{id}")
    public Res getDeletePersonManage(PersonalData personalData) throws Exception {
        if (personalDataService.getDeletePersonManage(personalData) != null) {
            return Res.create(1, "删除成功", personalData);
        } else {
            return Res.create(0, "删除失败", null);
        }
    }

    /**
     * 查询树状族系列表（第一代人列表）
     *
     * @return
     */
    @GetMapping("/checkinfo")
    public Res checkInfo(Integer adminId) {
        System.out.println("这是id" + adminId);
        if (adminId != null) {
            return Res.create(1, "族系列表查询成功", personalDataService.getList(adminId));
        } else {
            return Res.create(0, "无权限查看");
        }
    }

    /**
     * @param id
     * @return 已经炸开来，不想写了
     * 查询子代
     */
    @GetMapping("/checkchildreninfo")
    public Res checkchildreninfo(Integer id, Integer adminId) {
        List<PersonalData> personalData = personalDataService.checkchildreninfo(id, adminId);
        if (personalData != null) {
            return Res.create(1, "列祖列宗，查询成功", personalData);
        }
        return Res.create(0, "查询失败", null);
    }

    /**
     * 根据父代id查询子代
     *
     * @return
     */
    @GetMapping("/checkchildren")
    public Res checkChildren(Integer fatherId) {
        System.out.println("这是父亲id" + fatherId);
        if (fatherId != null) {
            return Res.create(1, "根据父代id已查出所有子代", personalDataService.getAllChildren(fatherId));
        } else {
            return Res.create(0, "无权限查看");
        }
    }

    /**
     * 根据父代id查询子代
     *
     * @return
     */
    @GetMapping("/checkcs")
    public Res getChildrenSpouse(Integer fatherId, Integer spouseId) {
//        System.out.println("这是父亲id"+fatherId);
        if (fatherId != null && spouseId != null) {
            return Res.create(1, "根据父代id已查出所有子孙及配偶", personalDataService.getChildrenSpouse(fatherId, spouseId));
        } else {
            return Res.create(0, "无权限查看");
        }
    }


}
